const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

// 定义简历模型
const Resume = sequelize.define('Resume', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  userId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    unique: true,
    references: {
      model: 'users',
      key: 'id'
    },
    onUpdate: 'CASCADE',
    onDelete: 'CASCADE'
  },
  fullName: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notEmpty: { msg: '姓名不能为空' },
      len: { args: [2, 20], msg: '姓名长度必须在2-20个字符之间' }
    }
  },
  gender: {
    type: DataTypes.ENUM('male', 'female', 'other'),
    allowNull: false
  },
  birthDate: {
    type: DataTypes.DATE,
    allowNull: false,
    validate: {
      isDate: { msg: '请输入有效的出生日期' }
    }
  },
  phone: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      isMobilePhone: { args: 'zh-CN', msg: '请输入有效的手机号码' }
    }
  },
  email: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      isEmail: { msg: '请输入有效的邮箱地址' }
    }
  },
  highestEducation: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notEmpty: { msg: '最高学历不能为空' }
    }
  },
  major: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notEmpty: { msg: '专业不能为空' }
    }
  },
  school: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notEmpty: { msg: '毕业院校不能为空' }
    }
  },
  graduationDate: {
    type: DataTypes.DATE,
    allowNull: false,
    validate: {
      isDate: { msg: '请输入有效的毕业日期' }
    }
  },
  workExperience: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  skills: {
    type: DataTypes.TEXT,
    allowNull: false,
    validate: {
      notEmpty: { msg: '技能不能为空' }
    }
  },
  projects: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  selfIntroduction: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  status: {
    type: DataTypes.BOOLEAN,
    defaultValue: true
  }
}, {
  tableName: 'resumes',
  timestamps: true,
  underscored: true,
  indexes: [
    { fields: ['user_id'] }
  ]
});

// 关联关系
Resume.associate = (models) => {
  // 简历属于用户
  Resume.belongsTo(models.User,
    { foreignKey: 'userId', as: 'user' });
};

module.exports = Resume;